package cn.song.controller;

import cn.song.dao.GirlRepository;
import cn.song.domain.Girl;
import cn.song.domain.ResponseResult;
import cn.song.service.GirlService;
import cn.song.utils.GirlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
public class GirlController {

    @Autowired
    private GirlRepository girlRepository;
    private Girl save;

    @Autowired
    private GirlService girlService;

    /**
     * 查询所有女生列表
     *
     * @return
     */
    @GetMapping(value = "/girls")
    public List<Girl> girlList() {

        return girlRepository.findAll();
    }

    /**
     * @param girl
     * @return
     */

    @PostMapping(value = "/addGirl")
    public ResponseResult<Girl> addgirl(@Valid Girl girl, BindingResult result) {
        if (result.hasErrors()) {
          //  System.out.println(result.getFieldError().getDefaultMessage());
         //    return null;
         return  GirlUtils.error(500,result.getFieldError().getDefaultMessage());
        }

        return GirlUtils.success(girl);

    }


    @GetMapping(value = "/girl/{id}")
    public Girl findgirl(@PathVariable("id") Integer id) {
        return girlRepository.findOne(id);
    }

    @PutMapping(value = "/genxinGirl")
    public Girl genxinGirl(Girl girl) {
        Girl save = girlRepository.save(girl);
        return save;
    }
    
    @GetMapping("/findGirlByage/{age}")
    public List<Girl> findByage(@PathVariable("age") Integer age) {
        return girlRepository.findByAge(age);
    }



    @GetMapping("/find/girl/{id}")
    public ResponseResult<Girl> findbyid(@PathVariable("id") Integer id){

        Girl findid = (Girl) girlService.findid(id);
        return  GirlUtils.success(findid);
    }

}
